#!/usr/bin/env python3

##################################################################################
##
## The MIT License (MIT)
##
## Copyright (c) 2015 Wang Ziqin <wangziqin1998@gmail.com>
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
##  of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included in
## all copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
## THE SOFTWARE.
##
##################################################################################

import sys
import random
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QLineEdit
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont


class NumBox(QLabel):
    '''数字显示区域'''

    def __init__(self):
        '''会抽出 [begin, end) 范围内的随机数'''
        QLabel.__init__(self)
        self.begin = 1
        self.end = 59 + 1
        self.setAlignment(Qt.AlignCenter)
        self.setMinimumHeight(180)

    def change(self):
        '''生成随机数并显示'''
        self.setText('<div style="font-family: Tahoma; \
            font-size: 120px">{}</div>'.\
            format(random.randrange(self.begin, self.end)))


class Window(QWidget):
    '''主窗口'''

    def __init__(self, title):
        QWidget.__init__(self)

        self.__ranbox = NumBox()
        self.__button = QPushButton('抽号')
        self.__sizetext = QLineEdit(str(self.__ranbox.end - 1))
        self.__button.setMaximumWidth(60)
        self.__button.setMinimumHeight(40)
        self.__button.setFont(QFont('Microsoft Yahei', 16))
        self.setFocus()
        self.__sizetext.setMaximumWidth(60)
        self.__sizetext.setMinimumHeight(40)
        self.__sizetext.setAlignment(Qt.AlignCenter)
        self.__sizetext.setFont(QFont('Segoe UI', 16))

        self.__button.clicked.connect(self.__ranbox.change)
        self.__sizetext.textChanged.connect(self.setrange)

        self.__Hlayout = QHBoxLayout()
        self.__Hlayout.addWidget(self.__sizetext)
        self.__Hlayout.addWidget(self.__button)

        self.__Vlayout = QVBoxLayout()
        self.__Vlayout.addWidget(self.__ranbox)
        self.__Vlayout.addLayout(self.__Hlayout)
        self.setLayout(self.__Vlayout)

        self.setWindowTitle(title) ## 设置标题栏
        # self.resize(250, 275)    ## 设置窗口大小
        self.adjustSize()

    def setrange(self):
    	self.__ranbox.end = int(self.__sizetext.text()) + 1


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Window('抽号')
    win.show()
    sys.exit(app.exec_())